package com.xiaomiMall.mange.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.xiaomiMall.mange.domain.XiaoMiProd;
import com.xiaomiMall.mange.domain.XiaoMiProdDetail;
import com.xiaomiMall.mange.domain.vo.Order;
import com.xiaomiMall.mange.domain.vo.XiaoMiProdOrderVo;
import com.xiaomiMall.mange.service.XiaoMiProdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author codefish
 * @date 2021/11/12
 * @apinote
 */
//2021年11月14日09:31:00
@RestController
@RequestMapping("/api/product/list")
public class XiaomiProductController extends BaseController {

    @Autowired
    private XiaoMiProdService xiaoMiProdService;

    @RequestMapping("/getProductList")
    public TableDataInfo getProductList(XiaoMiProd xiaoMiProd){
        startPage();
        List<XiaoMiProd> xiaoMiProdItem = xiaoMiProdService.getProdList(xiaoMiProd);
        //System.out.println("fuck:"+xiaoMiProdItem);
        return getDataTable(xiaoMiProdItem);
    }

    @RequestMapping("/getProductDetailList")
    public AjaxResult getProductDetailList(XiaoMiProd xiaoMiProd){

        List<XiaoMiProdDetail> xiaoMiProdDetailList = xiaoMiProdService.getProdDetailList(xiaoMiProd);

        /**
         * {prodId:'12', prodUrl:'www.baidu.com', prodVersion: ['3+23g', '6+64g', '8+128g']}
         */
        return AjaxResult.success(xiaoMiProdDetailList);
    }

    @PostMapping("/addProduct")
    public AjaxResult addProduct(@RequestBody JSONObject jsonObject){
        //System.out.println(jsonObject);
        JSONObject addInfo = jsonObject.getJSONObject("addInfo");
        //System.out.println(addInfo.getString("prodTitle"));
        JSONObject prodVersion = addInfo.getJSONObject("prodVersion");

        //System.out.println(jsonObject.getString("prodDesc"));
        //
        //System.out.println("length:"+prodVersion.size());

        //1.将数据插入基本表, 返回自增id
        XiaoMiProd xiaoMiProd = new XiaoMiProd();
        xiaoMiProd.setProdTitle(addInfo.getString("prodTitle"));
        xiaoMiProd.setProdDesc(addInfo.getString("prodDesc"));
        xiaoMiProd.setNid(addInfo.getString("prodCategory"));
        xiaoMiProdService.addProduct(xiaoMiProd);

        //2.将数据关联插入详情表
        List<XiaoMiProdDetail> xiaoMiProdDetailList = new ArrayList<>();

        //System.out.println("keys:"+prodVersion);
        prodVersion.forEach((key, value) ->{
            //System.out.println(key);
            //System.out.println(value);
            JSONObject priceAndCount = prodVersion.getJSONObject(key);
            String  prodPrice = priceAndCount.getString("prodPrice");
            String  prodCount = priceAndCount.getString("prodCount");
            XiaoMiProdDetail xiaoMiProdDetail = new XiaoMiProdDetail();
            //设置id
            xiaoMiProdDetail.setProdId(xiaoMiProd.getProdId());
            //设置版本
            xiaoMiProdDetail.setProdVersion(key);
            //设置产品当前价格
            xiaoMiProdDetail.setProdPriceCurrent(prodPrice);
            //设置产品原价(在当前价格之上加100元作为原价)
            xiaoMiProdDetail.setProdPricePre(String.valueOf(Integer.parseInt(prodPrice) + 100));
            //设置创建时间(自动生成)
            xiaoMiProdDetail.setProdCreateTime(DateUtils.getTime());
            //设置产品库存
            xiaoMiProdDetail.setProdCount(prodCount);
            //设置产品状态
            xiaoMiProdDetail.setStatus(addInfo.getString("status"));
            //System.out.println(xiaoMiProdDetail);
            xiaoMiProdDetailList.add(xiaoMiProdDetail);
        });

        boolean result = xiaoMiProdService.addProductDetail(xiaoMiProdDetailList);

        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //更新商品信息
    @PostMapping("/updateProduct")
    public AjaxResult updateProduct(@RequestBody JSONObject jsonObject){
        System.out.println(jsonObject);
        JSONObject addInfo = jsonObject.getJSONObject("updateInfo");
        JSONObject prodVersion = addInfo.getJSONObject("prodVersion");

        XiaoMiProd xiaoMiProd = new XiaoMiProd();
        xiaoMiProd.setProdId(addInfo.getString("prodId"));
        xiaoMiProd.setProdTitle(addInfo.getString("prodTitle"));
        xiaoMiProd.setProdDesc(addInfo.getString("prodDesc"));
        xiaoMiProd.setNid(addInfo.getString("prodCategory"));

        //2.将数据关联插入详情表
        List<XiaoMiProdDetail> xiaoMiProdDetailList = new ArrayList<>();

        //System.out.println("keys:"+prodVersion);
        prodVersion.forEach((key, value) ->{
            //System.out.println(key);
            //System.out.println(value);
            JSONObject priceAndCount = prodVersion.getJSONObject(key);
            String  prodPrice = priceAndCount.getString("prodPrice");
            String  prodCount = priceAndCount.getString("prodCount");
            XiaoMiProdDetail xiaoMiProdDetail = new XiaoMiProdDetail();
            //设置id
            xiaoMiProdDetail.setProdId(xiaoMiProd.getProdId());
            //设置版本
            xiaoMiProdDetail.setProdVersion(key);
            //设置产品当前价格
            xiaoMiProdDetail.setProdPriceCurrent(prodPrice);
            //设置产品原价(在当前价格之上加100元作为原价)
            xiaoMiProdDetail.setProdPricePre(String.valueOf(Integer.parseInt(prodPrice) + 100));
            //设置创建时间(自动生成)
            xiaoMiProdDetail.setProdCreateTime(DateUtils.getTime());
            //设置产品库存
            xiaoMiProdDetail.setProdCount(prodCount);
            //设置产品状态
            xiaoMiProdDetail.setStatus(addInfo.getString("status"));
            //System.out.println(xiaoMiProdDetail);
            xiaoMiProdDetailList.add(xiaoMiProdDetail);
        });

        boolean result = xiaoMiProdService.updateProduct(xiaoMiProd, xiaoMiProdDetailList);
        return AjaxResult.success();

    }

    //改变商品上架状态
    @GetMapping("/changeProdStatus")
    public AjaxResult changeProdStatus(String prodId, String status){
        boolean result = xiaoMiProdService.changeProdStatus(prodId, status);
        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //获取商品销售信息(根据时间)
    @PostMapping("/getOrderData")
    public AjaxResult getOrderData(Order prodOrder){
        List<XiaoMiProdOrderVo> orderList = xiaoMiProdService.getOrderData(prodOrder);
        List<HashMap<String, String>>  orderSalesInfo = xiaoMiProdService.getOrderSalesInfo();

        System.out.println(orderSalesInfo);
        return AjaxResult.success(orderSalesInfo);
    }

    //导出表格
    @PostMapping("/exportProduct")
    public AjaxResult exportProduct(@RequestBody JSONObject jsonObject){

        JSONArray jsonArray = jsonObject.getJSONArray("prodIds");

        String json = JSONObject.toJSONString(jsonArray, SerializerFeature.WriteClassName);
        List<String> prodIds = JSONObject.parseArray(json, String.class);
        List<XiaoMiProd> xiaoMiProds = xiaoMiProdService.exportProduct(prodIds);
        ExcelUtil<XiaoMiProd> xiaoMiProdExcelUtil = new ExcelUtil<>(XiaoMiProd.class);
        return xiaoMiProdExcelUtil.exportExcel(xiaoMiProds, "小米商品信息"+DateUtils.getDate(), "小米商品信息表");
    }


    //图片上传
    @PostMapping("/uploadPicture")
    public AjaxResult uploadPicture(@RequestParam("file") MultipartFile file, String prodId) throws IOException {
        if (!file.isEmpty()){

            //String fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + "jpg";
            //System.out.println("FileName:"+fileName);

            String prodPicture = FileUploadUtils.upload(XiaoMiProd.getProdPicturePath(), file);
            if (xiaoMiProdService.uploadProdPicture(prodPicture, prodId)){
                return AjaxResult.success("imagUrl", prodPicture);
            }
            return AjaxResult.success("imagUrl", prodPicture);
        }
        return AjaxResult.error();
    }
}
